#include <linux/proc_fs.h>
#include <linux/init.h>
#include <asm/uaccess.h>
#include <linux/string.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/irqreturn.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/cdev.h>


static unsigned char buff[100];
static struct class *hello_class;

//static int major;       		//主设备号
dev_t hello_dev;          		//保存分配的设备号
unsigned baseminor; 	  		//分配设备号的起始次设备号
static struct cdev hello_cdev;  //定义struct cdev 类型全局变量
unsigned char minor_count = 2; 	//指定次设备号个数


static int hello_open (struct inode *node, struct file *filp)
{
	printk("hello_open\n");
	printk("%s %s %d\n",__FILE__, __FUNCTION__, __LINE__);

	return 0;
}

static ssize_t hello_read (struct file *filp, char *buf, size_t size, loff_t *offset)
{
	size_t len = size > 100 ? 100 : size;
	printk("hello_read\n");
	copy_to_user(buf, buff, len);
	return len;
}

static ssize_t hello_write (struct file *filp, const char *buf, size_t size, loff_t *offset)
{
	size_t len = size > 100 ? 100 : size;
	memset(buff, 0 ,sizeof(buff));
	printk("hello_write\n");
	copy_from_user(buff, buf, len);
	return len;
}

static int hello_release (struct inode *node, struct file *filp)
{
	printk("hello_release\n");
	return 0;
}

/*1.定义 file_operations 结构体*/
static const struct file_operations hello_fops = {
    .owner 		= THIS_MODULE,
	.read		= hello_read,
	.write		= hello_write,
	.open		= hello_open,
	.release    = hello_release,
};


/*2.register_chrdev*/

/*3.入口函数*/
static int hello_init(void)
{
	struct device *dev;

	/*初始化设备方法1：自动分配设备号，占用所有次设备号*/
	//设备号
//	major = register_chrdev(0,"hello_drv",&hello_fops);

	/*初始化设备方法2：自动分配设备号，设置次设备号占用区域*/
	//1.自动分配设备号（起始次设备号baseminor，次设备数量 2）
	if(alloc_chrdev_region(&hello_dev, baseminor, minor_count,"hello_drv") < 0)
    {
        printk(KERN_ERR"Unable to alloc_chrdev_region.\n");
        return -EINVAL;
    }

	//2.initialize a cdev structure
	cdev_init(&hello_cdev, &hello_fops);
	
	//3.add a char device to the system
    if(cdev_add(&hello_cdev, hello_dev, minor_count) < 0)
    {
        printk(KERN_ERR "Unable to cdev_add.\n");
        return -EINVAL;
    }

	//4.register a range of device numbers
	register_chrdev_region(hello_dev, minor_count, "hello_drv");

	/*自动创建设备节点*/
	/*在内核中创建设备*/
	hello_class = class_create(THIS_MODULE, "hello_class");
	if (IS_ERR(hello_class)) {
		printk("hello class create failed!\n");
	}

	/*在/dev下面创建设备节点*/
	device_create(hello_class, NULL, hello_dev, NULL, "hello");
	if (IS_ERR(dev)) {
		printk("hello device_create  failed!\n");
	}
	
	return 0;
}


/*4.退出函数*/
static void hello_exit(void)
{
	//销毁设备
	device_destroy(hello_class, hello_dev);
	//删除设备类
	class_destroy(hello_class);

	/*对应初始化设备方法1：自动分配设备号，占用所有次设备号*/
//	unregister_chrdev(major,"hello_fops");

	/*对应初始化设备方法2：自动分配设备号，设置次设备号占用区域*/
	unregister_chrdev_region(hello_dev, minor_count);
	cdev_del(&hello_cdev);
 
}	

module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");



